* GRAPHS OF ORIGIN OF INCOMERS IN DIFFERENT TREATMENT AND CONTROL GROUPS
version 13.1

set more off, permanent
cap clear matrix
cap graph drop _all
cap log close

local rescue_mypathRR "$mypathRR"


* get the data with all the indicators&dummies needed
use "/Volumes/OW_data/my_dta_v8.1.dta", clear
label var zuzugjahre "move-in year (including several moves)"
label var zuzugjahr "move-in year (last move)"
drop if year>2010
drop if zuzugjahre>2010
bys year: egen residents=count(persid)

drop if zuzugkt==6
drop if zuzugjahre < 2001

* Adjust taxable income to account for the changes in deduction in 2008
replace esteuerbst=esteuerbst+10000 if year>=2008 // to account for the increased deduction under the flat-tax regime

set scheme mygraphs
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 


* GENERATE THE TREATMENT DEFINITION BASED ON RATE DETERMINING INCOME
gen taxinc_thre2 = (esatzbestkopf>= 300000 & esatzbestkopf <.)
label var taxinc_thre2 "Taxable income threshold regressive tax"

gen ctrl_ir_55_75 = (esatzbestkopf>= 300000*0.55 & esatzbestkopf< 300000*0.75)
label var ctrl_ir_55_75 "Control group: 55-75% of income threshold"

gen ctrl_ir_60_80 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.80)
label var ctrl_ir_60_80 "Control group: 60-80% of income threshold"

gen ctrl_ir_60_85 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.85)
label var ctrl_ir_60_85 "Control group: 60-85% of income threshold"

gen ctrl_ir_60_95 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.95)
label var ctrl_ir_60_95 "Control group: 60-95% of income threshold"

gen ctrl_ir_45_70 = (esatzbestkopf>= 300000*0.45 & esatzbestkopf< 300000*0.70)
label var ctrl_ir_45_70 "Control group: 40-70% of income threshold"

* Generate a dummy for each bracket
gen regbr1		= (esatzbestkopf>=300001 & esatzbestkopf<=340000)
gen regbr2		= (esatzbestkopf>=340001 & esatzbestkopf<=380000)
gen regbr3		= (esatzbestkopf>=380001 & esatzbestkopf<=420000)
gen regbr4		= (esatzbestkopf>=420001 & esatzbestkopf<=550000)
gen regbr5		= (esatzbestkopf>=550001 & esatzbestkopf<=600000)
gen regbr6		= (esatzbestkopf>=600001 & esatzbestkopf<=1000000)
gen regbr7		= (esatzbestkopf>=1000001 & esatzbestkopf<. )

* DEFLATE THE INCOME VARIABLES
merge m:1 year using "$mypathRR/Datasets/CPI.dta"
drop _merge
drop if year==.

foreach var in esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc mob_inc {
replace `var'=`var'/cpi*100.5 //deflate the series
local u : variable label `var'
local l= "`u' (real)" 
label var `var' "`l'"
}



gen avg_ntr=1-avgt_stek_taxable
label var avg_ntr "Average net-of-tax rate"
gen avg_ntr1=avg_ntr if year<2006
	label var avg_ntr1 	"Avg. NTR $t<2006$"
gen avg_ntr2=avg_ntr if year>2005 & year<2008
	label var avg_ntr2 	"Avg. NTR $t>=2006^*$"


* * * PREPARE SOME VARIABLES TO PROPERLY IDENTIFY INCOME AND WEALTH IN THE MOVE-IN YEAR AND STILL COUNT THE incomers CORRECTLY
sort persid year
egen freq= tag(persid zuzugjahre) //this will be used to select the incomers

foreach var in avg_ntr1 avg_ntr2  sex age married doppel snpanzki employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70 ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
  gen yrz=(year==zuzugjahre)
  replace yrz=yrz*(-1)
  gen `var'_yz=.
  bys persid (year): replace `var'_yz=`var' if yrz==-1
  bys persid _spell (yrz): replace yrz=yrz[1]
  bys persid (year): replace `var'_yz=`var'_yz[_n-1] if yrz==-1 & `var'_yz==.
  
  forvalues z=1/10 {
    local i=`z'
    bys persid (year): replace `var'_yz=`var'_yz[_n+1] if yrz==-1 & `var'_yz==. // note: repeat this step until 0 real changes made
    }
  drop yrz
}

foreach var in avg_ntr1 avg_ntr2  sex age married doppel snpanzki employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70 ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
			replace `var'=`var'_yz
}



cd "$mypathRR/Datasets/"
cap mkdir movers
cd "$mypathRR/Datasets/movers"

local b "60"
local t "80"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"




* Treatment, Control 2001-2010
preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
drop if zuzugjahre < 2001

xttab  zuzugkt if treat==1
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=1 if A1!=.
keep A*
tempfile Atreat
save "`Atreat'"

restore


preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
drop if zuzugjahre < 2001

xttab  zuzugkt if treat==0
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=0 if A1!=.

append using  "`Atreat'"
tempfile Adta
save "`Adta'"

rename A2 freq_o
rename A3 pct_o
rename A4 freq_b
rename A5 pct_b

restore


* Treatment, Control, All 2001-2010

* add the total
preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
drop if zuzugjahre < 2001

xttab  zuzugkt 
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=2 if A1!=.

append using  "`Adta'"

rename A2 freq_o
rename A3 pct_o
rename A4 freq_b
rename A5 pct_b

restore



* Treatment, Control before-after
preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2001 & zuzugjahre>2005

xttab  zuzugkt if treat==0 & zuzugjahre >= 2001 & zuzugjahre<=2005
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=10 if A1!=.
keep A*
tempfile Acontr_before
save "`Acontr_before'"

restore



preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2006 & zuzugjahre>2010

xttab  zuzugkt if treat==0 & zuzugjahre >= 2006 & zuzugjahre<=2010
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=9 if A1!=.
keep A*
tempfile Acontr_after
save "`Acontr_after'"

restore

preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2001 & zuzugjahre>2005

xttab  zuzugkt if treat==1 & zuzugjahre >= 2001 & zuzugjahre<=2005
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=12 if A1!=.
keep A*
tempfile Atreat_before
save "`Atreat_before'"

restore


preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2006 & zuzugjahre>2010

xttab  zuzugkt if treat==1 & zuzugjahre >= 2006 & zuzugjahre<=2010
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=11 if A1!=.

append using  "`Acontr_after'"
append using  "`Acontr_before'"
append using  "`Atreat_before'"
tempfile Bdta
save "`Bdta'"

rename A2 freq_o
rename A3 pct_o
rename A4 freq_b
rename A5 pct_b

restore


* add the total
preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2001 & zuzugjahre>2005

xttab zuzugkt if zuzugjahre >= 2001 & zuzugjahre<=2005
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=14 if A1!=.
keep A*
tempfile Ball_before
save "`Ball_before'"
restore


preserve
sort persid zuzugjahre year
keep if  freq==1
drop if zuzugkt==6
//drop if zuzugjahre < 2006 & zuzugjahre>2010

xttab zuzugkt if zuzugjahre >= 2006 & zuzugjahre<=2010
matrix A=r(results)
matrix list A
svmat A
label define cant 98 `"A"', modify
label define cant 99 `"?"', modify
label values A1 cant
gen A_treat=13 if A1!=.



append using  "`Bdta'"
append using  "`Ball_before'"
rename A2 freq_o
rename A3 pct_o
rename A4 freq_b
rename A5 pct_b

label define BFTREAT 10 `"control, t<2006"', modify
label define BFTREAT 9 `"control, t>=2006"', modify
label define BFTREAT 12`"treatment, t<2006"', modify
label define BFTREAT 11 `"treatment, t>=2006"', modify
label define BFTREAT 14 `"all, t<2006"', modify
label define BFTREAT 13 `"all, t>=2006"', modify
label values A_treat BFTREAT

keep A1 freq_o pct_o freq_b pct_b A_treat
drop if A1==.

save "origin_`x'_`y'_before-after.dta", replace

restore
cap drop treat_i
}
